package com.woniuxy01;

import java.util.Scanner;

/**
 * @author 作者
 * @create 2025-10-28 9:29
 */
public class Test01 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] a = {1, 3, 5, 23, 12, 78, 42, 0, 0};
        for (int i : a) {
            System.out.print(i + "\t");
        }
        System.out.println();
        int num, insertIndex, zeroIndex;
        String answer;
        do {
            zeroIndex = -1;
            System.out.println("输入要添加的元素");
            num = scanner.nextInt();
            System.out.println("输入要添加的元素的位置");
            insertIndex = scanner.nextInt();
            //判断是否需要扩容:查找是否存在0
            for (int i = 0; i < a.length; i++) {
                if (a[i] == 0) {
                    zeroIndex = i;
                    break;
                }
            }
            //没有0，要扩容
            if (zeroIndex == -1) {
                int[] b = new int[a.length + 1];
                for (int i = 0; i < a.length; i++) {
                    b[i] = a[i];
                }
                a = b;
            }
            //移动元素,从倒数第二位开始，到insertIndex，移动元素
            for (int i = a.length - 2; i >= insertIndex; i--) {
                a[i + 1] = a[i];
            }
            //插入元素
            a[insertIndex] = num;
            for (int i : a) {
                System.out.print(i + "\t");
            }
            System.out.println();
            System.out.println("是否继续添加y/n");
            answer = scanner.next();
        } while (answer.equals("y"));

    }
}
